package cn.iocoder.yudao.module.agent.controller.admin.brokerageclue;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.agent.controller.admin.brokerageclue.vo.*;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokerageclue.AgentBrokerageClueDO;
import cn.iocoder.yudao.module.agent.convert.brokerageclue.AgentBrokerageClueConvert;
import cn.iocoder.yudao.module.agent.service.brokerageclue.AgentBrokerageClueService;

@Tag(name = "管理后台 - 代理人分销用户线索")
@RestController
@RequestMapping("/agent/brokerage-clue")
@Validated
public class AgentBrokerageClueController {

    @Resource
    private AgentBrokerageClueService brokerageClueService;

    @PostMapping("/create")
    @Operation(summary = "创建代理人分销用户线索")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-clue:create')")
    public CommonResult<Long> createBrokerageClue(@Valid @RequestBody AgentBrokerageClueCreateReqVO createReqVO) {
        return success(brokerageClueService.createBrokerageClue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新代理人分销用户线索")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-clue:update')")
    public CommonResult<Boolean> updateBrokerageClue(@Valid @RequestBody AgentBrokerageClueUpdateReqVO updateReqVO) {
        brokerageClueService.updateBrokerageClue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除代理人分销用户线索")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('agent:brokerage-clue:delete')")
    public CommonResult<Boolean> deleteBrokerageClue(@RequestParam("id") Long id) {
        brokerageClueService.deleteBrokerageClue(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得代理人分销用户线索")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-clue:query')")
    public CommonResult<AgentBrokerageClueRespVO> getBrokerageClue(@RequestParam("id") Long id) {
        AgentBrokerageClueDO brokerageClue = brokerageClueService.getBrokerageClue(id);
        return success(AgentBrokerageClueConvert.INSTANCE.convert(brokerageClue));
    }

    @GetMapping("/list")
    @Operation(summary = "获得代理人分销用户线索列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-clue:query')")
    public CommonResult<List<AgentBrokerageClueRespVO>> getBrokerageClueList(@RequestParam("ids") Collection<Long> ids) {
        List<AgentBrokerageClueDO> list = brokerageClueService.getBrokerageClueList(ids);
        return success(AgentBrokerageClueConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得代理人分销用户线索分页")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-clue:query')")
    public CommonResult<PageResult<AgentBrokerageClueRespVO>> getBrokerageCluePage(@Valid AgentBrokerageCluePageReqVO pageVO) {
        PageResult<AgentBrokerageClueDO> pageResult = brokerageClueService.getBrokerageCluePage(pageVO);
        return success(AgentBrokerageClueConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出代理人分销用户线索 Excel")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-clue:export')")
    @OperateLog(type = EXPORT)
    public void exportBrokerageClueExcel(@Valid AgentBrokerageClueExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AgentBrokerageClueDO> list = brokerageClueService.getBrokerageClueList(exportReqVO);
        // 导出 Excel
        List<AgentBrokerageClueExcelVO> datas = AgentBrokerageClueConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "代理人分销用户线索.xls", "数据", AgentBrokerageClueExcelVO.class, datas);
    }

}
